<template>
  <div class="components-auditing">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('distributionMsg.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('users.disPhoneNumber')+':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('users.disPhoneNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionMsg.auditStatus')+':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('distributionMsg.auditStatus')"
            >
              <el-option
                :label="$t('product.pendingReview')"
                value="0"
              />
              <el-option
                :label="$t('productComm.pass')"
                value="1"
              />
              <el-option
                :label="$t('productComm.noPass')"
                value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="auditingTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="onSortChange"
        >
          <el-table-column
            :label="$t('users.disPhoneNumber')"
            prop="userMobile"
            width="180"
          />
          <el-table-column
            :label="$t('users.nickName')"
            prop="nickName"
            width="180"
          />
          <el-table-column
            :label="$t('users.parentPhoneNumber')"
            prop="parentUserMobile"
            width="180"
          >
            <template #default="scope">
              {{ scope.row.parentUserMobile || '-' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.invitees')"
            prop="parentNickName"
            width="180"
          >
            <template #default="scope">
              {{ scope.row.parentNickName || '-' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.accumulatedConsumptionAmount')"
            prop="sumOfConsumption"
            sortable
            width="160"
          >
            <template #default="scope">
              <span v-if="scope.row.sumOfConsumption === null">0</span>
              <span v-else>{{ scope.row.sumOfConsumption }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.accumulatedConsumptionTimes')"
            prop="expenseNumber"
            sortable
            width="160"
          >
            <template #default="scope">
              <span v-if="scope.row.expenseNumber === null">0</span>
              <span v-else>{{ scope.row.expenseNumber }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.applicationTime')"
            prop="auditingTime"
            sortable
            width="200"
          />
          <el-table-column
            :label="$t('distributionMsg.auditStatus')"
            prop="state"
          >
            <template #default="scope">
              <span v-if="scope.row.state === 0">{{ $t('product.pendingReview') }}</span>
              <span v-else-if="scope.row.state === 1">{{ $t('productComm.pass') }}</span>
              <span v-else-if="scope.row.state === -1">{{ $t('productComm.noPass') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribUserWallet.operator')"
            prop="sysUsername"
            align="center"
          >
            <template #default="scope">
              <span v-if="scope.row.sysUsername">{{ scope.row.sysUsername }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="120"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="
                    scope.row.state === 0 &&
                      isAuth('distribution:distributionAuditing:update')
                  "
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row, true)"
                >
                  {{ $t("productComm.audit") }}
                </div>
                <div
                  v-if="
                    scope.row.state !== 0 &&
                      isAuth('distribution:distributionAuditing:info')
                  "
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row, false)"
                >
                  {{ $t("seckill.view") }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <AuditingUpdate
      v-if="AuditingUpdateVisible"
      ref="AuditingUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AuditingUpdate from './auditing-update.vue'
import { isAuth } from '@/utils/index'

let tempSearchForm = null // 保存上次点击查询的请求条件
const searchForm = reactive({
  userMobile: '',
  state: '',
  nickName: null
})

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])

// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  const params = Object.assign(onSearch, {})
  // 日期赋值
  if (params && params.dateRange) {
    params.startTime = params.dateRange[0]
    params.endTime = params.dateRange[1]
    params.dateRange = null
  }

  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionAuditing/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10,
      sortParam,
      sortType
    }))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current

    dataList.value = data.records
  })
}
onMounted(() => {
  getDataList()
})

// 审核or查看审核信息
const AuditingUpdateRef = ref(null)
const AuditingUpdateVisible = ref(false)
const onAddOrUpdate = (data, isEdit) => {
  AuditingUpdateVisible.value = true
  nextTick(() => {
    AuditingUpdateRef.value?.init(data, isEdit)
  })
}
// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
// 刷新回调
const refreshChange = () => {
  // page = crud.$refs.tablePageRef.value?.defaultPage
  getDataList(page)
}

// 对应3种排序
let sortParam = 3// 0无 1积累消费金额 2积累消费笔数 3申请时间
let sortType = 2 // 0无 1 正序 2倒序
const onSortChange = (data) => {
  // 排序字段 0无 1积累消费金额 2积累消费笔数 3申请时间
  switch (data.prop) {
    case 'sumOfConsumption': sortParam = 1
      break
    case 'expenseNumber': sortParam = 2
      break
    case 'auditingTime': sortParam = 3
      break
    default: sortParam = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType = 2
      break
    case 'ascending': sortType = 1
      break
    default: sortType = 0
      break
  }
  getDataList(page)
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields()
}

</script>
